package com.li.springbootproject.service.rabbitmq;

import com.li.springbootproject.config.rabbitmq.RabbitMqConst;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 8/11/2022 8:51 AM
 */
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Service
public class RabbitMqServiceImpl implements RabbitMqService {

    @Resource(name = "noSingleRabbitTemplate")
    private RabbitTemplate prototypeRabbitTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public String testOne() {
        RabbitMqProduceConfirm confirm = new RabbitMqProduceConfirm();
        RabbitMqProduceReturnBack callback = new RabbitMqProduceReturnBack();
        prototypeRabbitTemplate.convertAndSend(RabbitMqConst.DEQUE_ONE, "hello world");
        prototypeRabbitTemplate.setReturnsCallback(callback);
        prototypeRabbitTemplate.setConfirmCallback(confirm);
        return null;
    }

    @Override
    public String testTwo() {
        RabbitMqProduceConfirm confirm = new RabbitMqProduceConfirm();
        RabbitMqProduceReturnBack callback = new RabbitMqProduceReturnBack();
        prototypeRabbitTemplate.setReturnsCallback(callback);
        prototypeRabbitTemplate.setConfirmCallback(confirm);
        prototypeRabbitTemplate.convertAndSend(RabbitMqConst.EXCHANGE_ONE_NAME, "", "我是交换机发出的消息");
        return null;
    }

    @Override
    public String testThree() {
        RabbitMqProduceConfirm confirm = new RabbitMqProduceConfirm();
        prototypeRabbitTemplate.setConfirmCallback(confirm);
        prototypeRabbitTemplate.convertAndSend(RabbitMqConst.DEQUE_THREE, "hello world");
        return null;
    }
}
